####################################################################################
## 得到上游处理好的文件
## 去除存在污染的样本
## vcf、maf、seg、mosdepth、msi
sh ${scripts_path}/module/get_genomic_file.sh

## 纯度低于0.1的样本，没有CNV改变，纯度赋值为1
cat ${Titan_path}/Purity_titan.final.tsv | awk -F'\t' '{OFS="\t"}{if($2 < 0.1){$2=1}print}' \
> ${Titan_path}/Purity_titan.final.reviseLowPurity.tsv

## 合并所有质控文件
${Rscript} ${scripts_path}/combineQC.R \
--msi_file ${Qc_path}/All_Msi.tsv \
--depth_file ${Qc_path}/Depth_summary.txt \
--info_file ${config_path}/tumor_normal.class.list \
--mutNum_file ${Qc_path}/Vcf_QC.list \
--out_file ${Qc_path}/Summary_Qc.tsv

## 合并纯度
${Rscript} ${scripts_path}/combinePurity.R \
--purity_file ${Titan_path}/Purity_titan.final.tsv \
--qc_file ${Qc_path}/Summary_Qc.tsv \
--info_file ${config_path}/tumor_normal.class.list \
--out_file ${Qc_path}/Summary_Qc.addPurity.tsv

####################################################################################
## 质控MSI的样本
## https://github.com/vanallenlab/MSIsensor/blob/master/README.md
## https://www.advancesinmolecularpathology.com/article/S2589-4080(21)00016-8/pdf
## Tumor和Normal配对的时候，MSI>3.5认为MSI-High
## 去除MSI-HIGH的样本
## 12个人的胃癌样本
## 11个人MSI>3.5
msi_high_sample=`cat ${Qc_path}/Summary_Qc.tsv | grep -v Tumor | awk -F'\t' '{if($8 > 10)print $2}' | sort -u |\
tr '\n' '|' | sed 's/|$//'`
## 1个人突变数量极高3680
## 有POLL、POLE、POLR2A、POLD1的CDS突变,MSI为2.93
## 也认为是超突变
msi_high_sample=`echo "${msi_high_sample}|JRGC00009"`

## 以下四个Tumor的样本,突变数量不到120个，纯度低于0.1
lowMutTumorSample="S37|JZGC00762|JZGC00750|S34"
## S37为DGC，去除以后剩下的为IM和DGC
## S34为IGC，原类型为IM + IGC + DGC，去除还存在IGC
cat ${config_path}/tumor_normal.class.bk.list | grep -v -E -w ${msi_high_sample} | grep -v -E -w ${lowMutTumorSample} |\
awk -F'\t' '{OFS="\t"}{if($1=="JZGC00580"){$6="IM + IGC"}print}' \
> ${config_path}/tumor_normal.class.list

## JZGC00580的类别进行替换
## IM+IGC+DGC替换为IM+IGC
cp ${Qc_path}/Summary_Qc.tsv ${Qc_path}/Summary_Qc.raw.tsv
cp ${Qc_path}/Summary_Qc.addPurity.tsv ${Qc_path}/Summary_Qc.addPurity.raw.tsv

cat ${Qc_path}/Summary_Qc.raw.tsv | awk -F'\t' '{OFS="\t"}{if($2=="JZGC00580"){$6="IM + IGC"}print}' \
> ${Qc_path}/Summary_Qc.tsv
cat ${Qc_path}/Summary_Qc.addPurity.raw.tsv | awk -F'\t' '{OFS="\t"}{if($2=="JZGC00580"){$6="IM + IGC"}print}' \
> ${Qc_path}/Summary_Qc.addPurity.tsv

####################################################################################
## Tree的文件，S65和JZGC00580各自去除一个样本
cat ${config_path}/tumor_normal.list | grep -E -w "S65|JZ580P1" | grep -v Normal | awk -F, '{print $2}' | sort -u | xargs -P 20 -I Normal sh -c '
echo Normal
sh ${scripts_path}/tree/Treeomics_CombineVcf_v4.sh Normal ${config_path}
'

####################################################################################
## 不考虑class，最标准的tumor+normal的格式
cat ${config_path}/tumor_normal.class.list | awk -F'\t' '{OFS=","}{print $3,$2}' \
> ${config_path}/tumor_normal.list

## IM的样本
im_sample=`cat ${config_path}/tumor_normal.class.list  | awk -F'\t' '{OFS="\t"}{if($4=="IM")print $3 }' | \
tr '\n' '|' | sed 's/|$//'`

## GC的样本
gc_sample=`cat ${config_path}/tumor_normal.class.list  | awk -F'\t' '{OFS="\t"}{if($4~"GC")print $3 }' | \
tr '\n' '|' | sed 's/|$//'`

cat ${config_path}/tumor_normal.list > ${config_path}/tumor_normal.all.list
cat ${config_path}/tumor_normal.list | grep -E -w "${im_sample}|Tumor" > ${config_path}/tumor_normal.precancer.list
cat ${config_path}/tumor_normal.list | grep -E -w "${gc_sample}|Tumor" > ${config_path}/tumor_normal.cancer.list

####################################################################################
#### 整理基线
## 整理年龄、性别、吸烟、饮酒等信息
## 注释整体和CDS的覆盖区域、倍体、纯度、质控信息
## 最新的添加了MSI患者的信息
<<EOF
${Rscript_mutationTime} ${scripts_path}/CompareBaseLine.R \
--tumor_list ${config_path}/tumor_normal.class.list \
--baseline_file ${config_path}/STAD_MutipleReigon_baseline.tsv \
--qc_file ${Qc_path}/Summary_Qc.tsv \
--burden_all_file ${Qc_path}/Burden.coverage10x.Autosomal.txt \
--burden_cds_file ${Qc_path}/Burden.coverage10x.Autosomal.cds.txt \
--purity_file ${Titan_path}/Purity_titan.final.tsv \
--out_dir ${config_path}
EOF

####################################################################################
## NMU的MAF文件得到
use_type=("cancer"  "precancer")
for class in ${use_type[@]}
do
echo $class
sh ${scripts_path}/module/mutsig2cv_coding.class.sh ${class}
sh ${scripts_path}/module/mutsig2cv_coding.class.MSI.sh ${class}
done

####################################################################################
#### CHAT
#### 计算CCF
sh ${scripts_path}/module/computeCCF.sh

####################################################################################
#### pyclone
#### 计算聚类
#### 基于单个样本
sh ${scripts_path}/module/computePyclone.SingleSample.sh

####################################################################################
#### Mutationtime
#### 计算突变时间
sh ${scripts_path}/module/computeMutaionTime.sh

####################################################################################
## 分病理类型合并GGA以后的MAF
export use_type=("all" "cancer"  "precancer")

for class in ${use_type[@]}
do
echo $class
## 头行注释
cat ${maf_path}/*_GGA_Filter_funcotated.maf | grep -v "#" | head -1 > ${maf_path}/All_GGA.${class}.maf
for line in `cat ${config_path}/tumor_normal.${class}.list | grep -v Tumor`
do
Tumor=`echo ${line} | awk -F, '{print $1}'`
Normal=`echo ${line} | awk -F, '{print $2}'`
cat ${maf_path}/${Tumor}_${Normal}_GGA_Filter_funcotated.maf | grep -v -E "##|Hugo_Symbol|#version"  | \
awk -F'\t' '{OFS="\t"}{$16=Tumor;print}' Tumor=${Tumor} \
>> ${maf_path}/All_GGA.${class}.maf
done
done

## MSI的患者
cat ${maf_path}/*_GGA_Filter_funcotated.maf | grep -v "#" | head -1 > ${maf_path}/All_GGA.all.MSI.maf
for line in `cat ${config_path}/tumor_normal.MSI.list | grep -v Tumor`
do
Tumor=`echo ${line} | awk -F, '{print $1}'`
Normal=`echo ${line} | awk -F, '{print $2}'`
cat ${maf_path}/${Tumor}_${Normal}_GGA_Filter_funcotated.maf | grep -v -E "##|Hugo_Symbol|#version"  | \
awk -F'\t' '{OFS="\t"}{$16=Tumor;print}' Tumor=${Tumor} \
>> ${maf_path}/All_GGA.all.MSI.maf
done


####################################################################################
##################################### 突变负荷计算
## NJMU
## 使用QC的maf文件
sh ${scripts_path}/mutBurden/mutBurden_getmaf.sh

<<EOF
## IM、IGC和DGC的突变负荷的比较
## 同一个人的比较
${Rscript} ${scripts_path}/mutBurden/mutBurden_plot.R \
--info_file ${config_path}/STAD-useCombine.Sample.tsv \
--maf_file ${maf_path}/All_ForMutBurden.extract.maf \
--images_path ${Images_path}/mutBurden
EOF

##################################### 突变负荷和基线的比较
## 其它数据无对应信息仅在NMU中进行比较
<<EOF
ln -snf ${Images_path}/mutBurden/mutBurden.tsv ${baseTable_path}/STAD_Info.addBurden.tsv
## 突变负荷与年龄的关系
## 年龄为连续性变量
${Rscript} ${scripts_path}/mutBurden/mutBurden_plot.Age.R \
--input_file ${baseTable_path}/STAD_Info.addBurden.tsv \
--images_path ${Images_path}/mutBurden

## 突变负荷与性别、吸烟、饮酒、HP的关系
${Rscript} ${scripts_path}/mutBurden/mutBurden_plot.Baseline.R \
--input_file ${baseTable_path}/STAD_Info.addBurden.tsv \
--images_path ${Images_path}/mutBurden
EOF

####################################################################################
##################################### 基因突变的共享情况(MSS)
## 以人为单位，在几个人中为癌前和癌共享的突变，在几个人中为癌前和癌私有的突变(功能性突变)
${Rscript} ${scripts_path}/plot/mutShare_compute.R \
--maf_file ${maf_path}/All_GGA.all.maf \
--images_path ${Images_path}/mutRate \
--info_file ${config_path}/tumor_normal.class.list

## 多次出现突变位点的共享情况
${Rscript} ${scripts_path}/plot/mutShare_compute.RecurrentPoint.R \
--maf_file ${maf_path}/All_GGA.all.maf \
--recurrent_point_file ${Images_path}/mutRate/MutRate.RecurrentPoint.tsv \
--images_path ${Images_path}/mutRate \
--info_file ${config_path}/tumor_normal.class.list

## 所有突变位点计算是否共享
## 用于突变信号分类
## CDS
${Rscript} ${scripts_path}/plot/mutShare_compute.AllPoint.R \
--maf_file ${maf_path}/All_GGA.all.maf \
--images_path ${Images_path}/mutRate \
--info_file ${config_path}/tumor_normal.class.list

## 所有
${Rscript} ${scripts_path}/plot/mutShare_compute.AllPoint.AllMuts.R \
--maf_file ${maf_path}/All_GGA.all.maf \
--images_path ${Images_path}/mutRate \
--info_file ${config_path}/tumor_normal.class.list

####################################################################################
##################################### 基因突变的共享情况(MSI)
## 以人为单位，在几个人中为癌前和癌共享的突变，在几个人中为癌前和癌私有的突变(功能性突变)
${Rscript} ${scripts_path}/plot/mutShare_compute.R \
--maf_file ${maf_path}/All_GGA.all.MSI.maf \
--images_path ${Images_path}/mutRateMSI \
--info_file ${config_path}/tumor_normal.class.MSI.list

## 所有突变位点计算是否共享
## 用于突变信号分类
${Rscript} ${scripts_path}/plot/mutShare_compute.AllPoint.R \
--maf_file ${maf_path}/All_GGA.all.MSI.maf \
--images_path ${Images_path}/mutRateMSI \
--info_file ${config_path}/tumor_normal.class.MSI.list

## 所有
${Rscript} ${scripts_path}/plot/mutShare_compute.AllPoint.AllMuts.R \
--maf_file ${maf_path}/All_GGA.all.MSI.maf \
--images_path ${Images_path}/mutRateMSI \
--info_file ${config_path}/tumor_normal.class.MSI.list


##################################### NMU中驱动基因的检查
## 计算突变率
<<EOF
use_type=("cancer"  "precancer")
for class in ${use_type[@]}
do
echo $class
${Rscript} ${scripts_path}/mutsig/Mutsig_check.R \
--class ${class} \
--sig_file ${MutsigOut_path}/${class}/sig_genes.txt \
--smg_file ${work_dir}/public_ref/SMG_sort.list \
--mutRate_file ${Images_path}/mutRate/MutRate.tsv \
--out_path ${mutsig_check_path}/${class} 
done

## 每个基因检查突变是否可靠
for class in ${use_type[@]}
do
echo $class
for gene in `cat ${mutsig_check_path}/${class}/mutRate.${class}.tsv | sed '1d' | awk -F"\t" '{print $1}'`
do
${Rscript} ${scripts_path}/plot/Lollipop_variant.R \
--gene ${gene} \
--pre_file ${maf_path}/All_GGA.precancer.maf \
--cancer_file ${maf_path}/All_GGA.cancer.maf \
--sample_info ${config_path}/tumor_normal.class.list \
--gtf_file ${ref_path}/GTF/gencode.v19.annotation.exonNum.gtf \
--out_path ${mutsig_check_path}/${class}/${gene}
done
done
EOF

####################################################################################
##################################### 突变信号计算
###### NMU看总体和共享的突变信号
## 1、基于GGA，分为总体突变信号、trunk突变信号和private突变信号
## 分别产生对应的vcf文件
rm -rf ${SigProfiler_path}/vcf/

cat ${config_path}/tumor_normal.list | tr ',' ' ' | grep -v Normal | xargs -P 10 -i sh -c '
echo {}
sh ${scripts_path}/sigprofile/denovoSig_1_getPassVcf.sh {}
'

## 2、提取突变信号
## 进入python中可以运行，直接运行脚本会有Bug
${python} ${scripts_path}/sigprofile/denovoSig_2_SigProfiler.all.py
${python} ${scripts_path}/sigprofile/denovoSig_2_SigProfiler.trunk.py
${python} ${scripts_path}/sigprofile/denovoSig_2_SigProfiler.private.py

## 3、denovoToDecompose
share_list=("trunk" "private" )
for share in ${share_list[@]}
do
${python} ${scripts_path}/sigprofile/denovoSig_3_decompose.py \
${SigProfiler_path}/extractor/${share}
done

## 4、decompose的文件链接
for share in ${share_list[@]}
do
echo ${share}
## decompose的信号组成
cp -rf ${SigProfiler_path}/extractor/${share}/decompose/SBS96/Decompose_Solution/Activities/Decompose_Solution_Activities.txt \
${SigProfiler_path}/decompose/${share}_SBS96.txt
## 总体的突变数量
cp -rf ${SigProfiler_path}/extractor/${share}/SBS96/Samples.txt ${SigProfiler_path}/decompose/${share}_SBS96.AllMuts.txt
done

## 5、突变信号分布画图
## 总的
${Rscript} ${scripts_path}/sigprofile/Sigprofiler_decompose_plot_v2.R \
--work_dir ${SigProfiler_path}/decompose/ \
--base_line_file ${config_path}/tumor_normal.class.list \
--images_path ${SigProfiler_path}/plot

## IGC和DGC合并为胃癌
${Rscript} ${scripts_path}/sigprofile/Sigprofiler_decompose_plot_v2.GC.R \
--work_dir ${SigProfiler_path}/decompose/ \
--base_line_file ${config_path}/tumor_normal.class.list \
--images_path ${SigProfiler_path}/plot

## IM分为不同来源
${Rscript} ${scripts_path}/sigprofile/Sigprofiler_decompose_plot_v2.IM.R \
--work_dir ${SigProfiler_path}/decompose/ \
--base_line_file ${config_path}/tumor_normal.class.list \
--images_path ${SigProfiler_path}/plot

## 每个人的画突变信号的分布，看突变信号是在总的样本中如此还是个别样本的情况
${Rscript} ${scripts_path}/sigprofile/Sigprofiler_decompose_plot_v2.everysample.R \
--work_dir ${SigProfiler_path}/decompose/ \
--base_line_file ${config_path}/tumor_normal.class.list \
--images_path ${SigProfiler_path}/plot

####################################################################################
